<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{
    public function index()
    {

        $this->display();
		
    }
	
	//显示
	public function select()
	{
		
		$mod=M("stu");
		//判断并封装搜索条件
        $where=array();
        if(!empty($_GET['name'])){
            $map['name']=array("like","%{$_GET['name']}%");
			$this->assign("name",$_GET['name']);
		}
		if($_GET['sex']==="0" || !empty($_GET['sex']) ){
            $map['sex']=array("like","%{$_GET['sex']}%");
			$this->assign("sex",$_GET['sex']);
		}
        //实例化分页类
		//定义每页显示的条数
		$m=5;
        $page = new \Think\Page($mod->where($map)->count(),$m);
	    $list=$mod->where($map)->order("id desc")->limit($page->firstRow.",".$page->listRows)->select();
		$this->assign("list",$list);
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		

		
		$this->assign("pageinfo",$page->show());
		$this->display();
	}
	
	//加载添加模板
	public function add()
	{
		$this->display();
	}
	
	//加载添加方法
	public function insert()
	{
		$upload = new \Think\Upload();// 实例化上传类 
		$upload->maxSize   =     3145728 ;// 设置附件上传大小 
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
		$upload->savePath  =      '../Public/Uploads/'; // 设置附件上传目录    
		$upload->autoSub  =      false; // 设置附件上传目录    
		// 上传文件   
		$info   =   $upload->upload();   
		if(!$info) {// 上传错误提示错误信息   
		$this->error($upload->getError());  
		}else{// 上传成功      
		
		$image = new \Think\Image(); 
		$image->open("./public/Uploads/{$info['pic']['savename']}");
	
		$image->thumb(50, 50)->save("./public/Uploads/s_{$info['pic']['savename']}");

		}
		
		
		 $mod=M("stu");
		 $data=array();
		 $data['pic']=$info['pic']['savename'];
		 $data['name']=$_POST['name'];
		 $data['age']=$_POST['age'];
		 $data['sex']=$_POST['sex'];
		 $data['class']=$_POST['class'];
		
		 $mod->create($data);
		 $result=$mod->add();
		 if($result+0>0){   
			$this->success('新增成功', 'select',0);
		 } else {   
			$this->error('新增失败');
		 }
	}
	
	//加载修改模板
	public function edit($id)
	{
		$mod=M("stu");
		$list=$mod->find("{$id}");
		$this->assign("list",$list);
		$this->display();
	}
	
	//执行修改信息的方法
	public function update()
	{
		
		if($_FILES['pic']['error']!=4){
			$upload = new \Think\Upload();// 实例化上传类 
			$upload->maxSize   =     3145728 ;// 设置附件上传大小 
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
			$upload->savePath  =      '../Public/Uploads/'; // 设置附件上传目录    
			$upload->autoSub  =      false; // 设置附件上传目录    
			// 上传文件   
			$info   =   $upload->upload();   
			if(!$info) {// 上传错误提示错误信息   
			$this->error($upload->getError());  
			}else{// 上传成功      
			
				$image = new \Think\Image(); 
				$image->open("./public/Uploads/{$info['pic']['savename']}");
			
				$image->thumb(50, 50)->save("./public/Uploads/s_{$info['pic']['savename']}");
				$mod=M("stu");
				$pic=$_POST['picname'];
				unset($_POST['picname']);
				$_POST['pic']=$info['pic']['savename'];
				$mod->create($_POST);
				$result=$mod->save();
				if($result+0>0){
					$this->success('修改成功', 'select',0);
					//删除原图片
					unlink("./public/uploads/s_{$pic}");
					unlink("./public/uploads/{$pic}");
				} else {   
					unlink("./public/uploads/s_{$info['pic']['savename']}");
					unlink("./public/uploads/{$info['pic']['savename']}");
					$this->error('修改失败');
				}
			}
			
				
		}else{
			$mod=M("stu");
			$_POST['pic']=$_POST['picname'];
			unset($_POST['picname']);
			$mod->create($_POST);
			$result=$mod->save();
			if($result+0>0){
				$this->success('修改成功', 'select',0);
			} else {   
				$this->error('修改失败');
				
			}
		}
		
	}
	
	
	//执行修改信息的方法
	 public function del($id,$pic)
	 {
		 $mod=M("stu");
		 $result=$mod->delete($id);
		 if($result>0){
			 unlink("./public/uploads/{$pic}");
			 unlink("./public/uploads/s_{$pic}");
			 $this->success('删除成功', 'select',0);
		 } else {   
			 $this->error('删除失败');
		 }
	 }
	 
	 public function pic($pic)
	 {
		 $this->assign("pic",$pic);
		 $this->display();
		 
	 }
	 
	 public function addemail()
	 {
		 $this->display();
	 }
	 //执行发送邮箱的办法
	 public function email()
	 { 
	     $mail='<a  href="//www.cts666.com">'.$_POST['content'].'</a>';
		if(SendMail($_POST['email'],$_POST['title'],$mail)){
			$this->success('发送成功！');
		}else{
			$this->error('发送失败');
		}

     }


     public function ip()
     {
     	$ip = get_client_ip();
     	$Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        $area = $Ip->getlocation('117.100.17.218'); // 获取某个IP地址所在的位置
     	var_dump($area);
     }
	 
	 
	 public function addpic()
	 {
		$this->display(); 
	 }
	 
	 
	 public function uploadpic()
	 {
		if($_FILES['pic']['error']!=4){ 
			$upload = new \Think\Upload();// 实例化上传类 
			$upload->maxSize   =     3145728 ;// 设置附件上传大小 
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
			$upload->savePath  =      '../Public/Uploads/'; // 设置附件上传目录    
			$upload->autoSub  =      false; // 设置附件上传目录    
			// 上传文件   
			$info   =   $upload->upload();   
			if(!$info) {// 上传错误提示错误信息   
				$this->error($upload->getError());  
			}else{// 上传成功      
			
				$image = new \Think\Image(); 
				$image->open("./public/Uploads/{$info['pic']['savename']}");
			
				$image->thumb(350, 350)->save("./public/Uploads/s_{$info['pic']['savename']}");

			}
		}else{
			$this->error('Index/addpic',"请选择图片");
		}
	 }

     
	 
	 
	 
	
}